package com.greydawn.PrototypePattern;

/**
 * @Author: zhongwr
 * @Create: 2021/12/12 15:15
 * @Version 1.0
 * @Description:
 */
public abstract class People implements Cloneable {

    protected String name;
    protected Integer age;

    protected Address address;

    @Override
    protected Object clone() {
        Object obj = null;
        try {
            // 此克隆是浅拷贝，即只拷贝字段值，引用对象拷贝引用，
            // 若要实现深拷贝(拷贝出来的对象的引用类型字段独立)需要重写clone方法
            obj = super.clone();
        } catch (CloneNotSupportedException e) {
            // 捕获掉异常，不然调用要抛出异常很麻烦
        }
        return obj;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public abstract void doSomething();

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }
}
